
package com.hlkj.pay.app.merchant.ext.impl.hnapay.handler;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import cn.hutool.json.JSONObject;
import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.framework.common.util.json.JsonUtils;
import com.hlkj.pay.app.merchant.ext.dto.access.ChannelMerchantBusinessContext;
import com.hlkj.pay.app.merchant.ext.impl.hnapay.constant.HnapayConstant;
import com.hlkj.pay.app.merchant.ext.impl.hnapay.dto.HnapayConfig;
import com.hlkj.pay.app.merchant.ext.impl.hnapay.utils.HttpUtils;
import com.hlkj.pay.app.merchant.ext.impl.hnapay.utils.RecepitUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author HlpayTeam
 * @date 2024/10/16 9:10
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class HnapayMerchantAccessInfoHandler {

    private final HnapayMerchantCommonParamsHandle paramsHandle;

    /**
     * 商户余额查询
     * 实现暂不使用 没有测试  有问题
     * @param channelMerchantBusinessContext
     */
    public void queryBalance(ChannelMerchantBusinessContext channelMerchantBusinessContext) {
        CommonResult<HnapayConfig> payConfigCommonResult = paramsHandle.channelConfig(channelMerchantBusinessContext.getProviderChannelConfig());
        // 配置转换
        if (!payConfigCommonResult.isSuccess()) {
            channelMerchantBusinessContext.setRemark(payConfigCommonResult.getMsg());
            channelMerchantBusinessContext.setProcessStatus(Boolean.FALSE);
            return;
        }
        HnapayConfig payConfig = payConfigCommonResult.getData();
        String requestUrl = HnapayConstant.DOMAIN_PROD + HnapayConstant.QUERY_BALANCE;

        try {
            Map<String, String> bodyMap = new LinkedHashMap<>();
            bodyMap.put("version", "2.0");
            bodyMap.put("tranCode", "QB01");
            bodyMap.put("merId", payConfig.getMerId());
            bodyMap.put("acctType", "");
            bodyMap.put("charset", "1");
            bodyMap.put("signType", "3");
            bodyMap.put("signValue", RecepitUtil.sign(payConfig.getMchM2PrivateKey(), bodyMap));

            log.info("新生账户余额查询req参数:{}", JsonUtils.toJsonString(bodyMap));
            String response = HttpUtils.postForm(requestUrl, bodyMap);
            log.info("新生账户余额查询返回参数:{}", response);
            JSONObject respObj = JsonUtils.parseObj(response);
            if ("0000".equals(respObj.getStr("resultCode"))) {
                //账户可用余额
                String avaBalance = respObj.getStr("avaBalance");
                //待结转金额
                String pendAmt = respObj.getStr("pendAmt");
            } else {
                channelMerchantBusinessContext.setRemark(respObj.getStr("errorMsg"));
                log.error("新生账户余额查询失败 message: {}", respObj.getStr("errorMsg"));
            }
        } catch (Exception e) {
            log.error("新生账户余额查询请求失败 message:{}", e.getMessage(), e);
            channelMerchantBusinessContext.setRemark("新生账户余额查询请求失败,失败信息" + e.getMessage());
            channelMerchantBusinessContext.setProcessStatus(Boolean.FALSE);
        }

    }

}
